package com.yidu.ruifeng_logistics.filter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @author: 肖楚科
 * @date:2021/03/17
 * @time:19:17
 * @blog:${PACKAGE_NAME}
 * @description:
 */
@WebFilter("/1231231")
public class LoginFilter implements Filter {

    Logger logger = LoggerFactory.getLogger(this.getClass());
    public void destroy() {
    }

    public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
        HttpSession session = request.getSession();
        String url = request.getRequestURI();
        logger.info("资源路径为:" + url);
        Object login = session.getAttribute("login");
        if (url.contains("login.")||url.contains("loginOrRegister")||url.contains("css")||url.contains("image")
        ||url.contains("layui")||url.contains("jquery.js")||url.contains("register.")){
            chain.doFilter(req, resp);
        }else if (login!=null){
            chain.doFilter(req, resp);
        }else {
            response.sendRedirect("/pages/LoginOrRegister/login.html");
        }
    }

    public void init(FilterConfig config) throws ServletException {

    }

}

